#include "StdAfx.h"
#include "PolygonObj.h"


CPolygonObj::CPolygonObj(void):CShape()
{
	m_shapeStyle = SHAPE_POLYGON;
	m_dblR=0.0;
	m_dblG=0.0;
	m_dblB=0.0;
	m_width=0.5;
	m_dblFillColorR =0;
	m_dblFillColorG =0;
	m_dblFillColorB =0;
	szUnit=_T("");
	szPartId=_T("");
	IsChangeColor=false;
	m_bIsPointer = false;
}

CPolygonObj::CPolygonObj(CEntityObj *pParentEnt)
	:CShape(pParentEnt)
{
	m_shapeStyle = SHAPE_POLYGON;
	m_dblR=0.0;
	m_dblG=0.0;
	m_dblB=0.0;
	m_width=0.5;
	m_dblFillColorR =0;
	m_dblFillColorG =0;
	m_dblFillColorB =0;
	szUnit=_T("");
	szPartId=_T("");
	IsChangeColor=false;
	m_bIsPointer = false;
}

CPolygonObj::CPolygonObj(const  CPolygonObj* pSrc ):CShape(pSrc)
{
	m_shapeStyle = pSrc->getShapeStyle();

	m_dblR=pSrc->GetColorR();
	m_dblG=pSrc->GetColorG();
	m_dblB=pSrc->GetColorB();
	m_width=pSrc->GetWidth();	
	m_dblFillColorR =pSrc->GetFillColorR();
	m_dblFillColorG =pSrc->GetFillColorG();
	m_dblFillColorB =pSrc->GetFillColorB();
	vector<HPoint> pts = pSrc->GetAllPoints();
	for (vector<HPoint>::iterator iter = pts.begin();iter!=pts.end();iter++)
	{
		m_HPoints.push_back(*iter);
	}
	szUnit=pSrc->GetUnit();
	szPartId=pSrc->GetPartId();
	IsChangeColor=pSrc->GetIsChangeColor();
	m_bIsPointer = pSrc->IsPointer();
}

CPolygonObj::~CPolygonObj(void)
{
}

void CPolygonObj::Serialize( CArchive & ar )
{
	if (GetIsSvgGroundShape()||GetIsSvgComponentShape())
	{
		if (ar.IsStoring())
		{
			ar<<m_dblR;
			ar<<m_dblG;
			ar<<m_dblB;
			ar<<m_width;
			ar<<m_dblFillColorR;
			ar<<m_dblFillColorG;
			ar<<m_dblFillColorB;
			int nSize = m_HPoints.size();
			ar << nSize;
			for (vector<HPoint>::iterator iter1=m_HPoints.begin(); 
				iter1!=m_HPoints.end(); iter1++)
			{
				HPoint point = *iter1;
				ar << point.x;
				ar << point.y;
				ar << point.z;
			}	
			CObj::WriteString(ar,szPartId);
			CObj::WriteString(ar,szUnit);
			ar<<IsChangeColor;
			if (CObj::m_lFileVersion>78)
			{
				ar<<m_bIsPointer;
			}
		} 
		else
		{
			ar >>m_dblR;
			ar >>m_dblG;
			ar >>m_dblB;
			ar >>m_width;
			ar >>m_dblFillColorR;
			ar >>m_dblFillColorG;
			ar >>m_dblFillColorB;
			int nSize(0);
			ar >> nSize;
			for (int i=0; i<nSize; i++)
			{
				float x,y,z;
				ar >> x;
				ar >> y;
				ar >> z;
				HPoint point = new HPoint(x,y,z);
				m_HPoints.push_back(point);
			}	
			CObj::ReadString(ar,szPartId);
			CObj::ReadString(ar,szUnit);
			ar>>IsChangeColor;
			if (CObj::m_lFileVersion>78)
			{
				ar>>m_bIsPointer;
			}
		}
	}
	CShape::Serialize(ar);
}

CDisplayObj * CPolygonObj::CreateDisplayObj( long lLayerId )
{
	CPolygonDisplayObj *pPolygonDis = new CPolygonDisplayObj(this);
	pPolygonDis->SetLayerId(lLayerId);
	return pPolygonDis;
}


void CPolygonObj::CopyDisplayObj( long lSourceLayerId, long lDestLayerId )
{
	CEntityObj::CopyDisplayObj(lSourceLayerId, lDestLayerId);
}

void CPolygonObj::DeleteDisplayObj( long lLayerId )
{
	CEntityObj::DeleteDisplayObj(lLayerId);
}

void CPolygonObj::CalcBox()
{
	vector<HPoint> vecHPoints=GetAllPoints();
	vector<HPoint>::iterator it = vecHPoints.begin();
	m_minX=m_maxX=(*it).x;
	m_minY=m_maxY=(*it).y;
	for (;it!=vecHPoints.end();it++)
	{
		HPoint point = *it;
		if (m_minX > point.x)
			m_minX = point.x;
		if (m_maxX < point.x)
			m_maxX = point.x;
		if (m_maxY < point.y)
			m_maxY = point.y;
		if (m_minY > point.y)
			m_minY = point.y;
	}
}

void CPolygonObj::SetFillColor( double colorR, double colorG, double colorB )
{
	m_dblFillColorR = colorR;
	m_dblFillColorG = colorG;
	m_dblFillColorB = colorB;
}

void CPolygonObj::SetColor( double colorR, double colorG, double colorB )
{
	m_dblR=colorR; 
	m_dblG=colorG;
	m_dblB=colorB;
}

CObj * CPolygonObj::Clone() const
{
	return new CPolygonObj(this);
}

// void CPolygonObj::SetAllPoints(vector<HPoint> &ptVec)
// {
// 	m_HPoints.clear();
// 	for (vector<HPoint>::iterator iter = ptVec.begin();iter!=ptVec.end();iter++)
// 	{
// 		m_HPoints.push_back(*iter);
// 	}m_HPoints.assign()
// }
